package com.everflourish.yeah100.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.everflourish.yeah100.R;
import com.everflourish.yeah100.act.questionlib.QuestionImageActivity;
import com.everflourish.yeah100.http.ImageRequest;
import com.everflourish.yeah100.utils.IntentUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Created by liqisi on 2016/7/20.
 * 时间：2016/7/20
 */
public class QuestionImageAdapter extends RecyclerView.Adapter<QuestionImageAdapter.QuestionImageHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private ArrayList<String> mList;
    private ImageRequest imageRequest;
    private RequestQueue mQueue;

    public QuestionImageAdapter(Context mContext, List<String> mList) {
        this.mContext = mContext;
        this.mList = (ArrayList<String>) mList;
        mLayoutInflater = LayoutInflater.from(mContext);
        mQueue = Volley.newRequestQueue(mContext);
        imageRequest = ImageRequest.getInstance(mQueue);
    }

    @Override
    public QuestionImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_question_image_item, parent, false);
        QuestionImageHolder viewHolder = new QuestionImageHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(QuestionImageHolder holder, final int position) {
        String imageUrl = mList.get(position);
        imageRequest.get(holder.itemImage, imageUrl);
        holder.itemImage.setOnClickListener(new MyOnclickListener(holder, position, imageUrl));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class QuestionImageHolder extends RecyclerView.ViewHolder {

        private ImageView itemImage;

        public QuestionImageHolder(View itemView) {
            super(itemView);
            itemImage = (ImageView) itemView.findViewById(R.id.item_question_image);
        }
    }

    class MyOnclickListener implements View.OnClickListener {

        private QuestionImageHolder viewHolder;
        private int position;

        public MyOnclickListener(QuestionImageHolder viewHolder, int position, String imageUrl) {
            this.viewHolder = viewHolder;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.item_question_image:
                    Intent questionIntent = new Intent(mContext,
                            QuestionImageActivity.class);
                    questionIntent.putExtra(IntentUtil.QUESTIONLISTIMAGE, mList);
                    questionIntent.putExtra(IntentUtil.QUESTIONLISTIMAGEINDEX, position);
                    mContext.startActivity(questionIntent);
                    break;
            }
        }
    }
}